module controller.category_ctrl;

import vibe.http.server : HTTPServerRequest, HTTPServerResponse;
import vibe.data.json : Json, serializeToJson;

import service.article_service;
import service.cate_service;

import darktemple.render;

/** 
 * 某分类下所有文章页
 */
void getCateArticlesPage (scope HTTPServerRequest req, scope HTTPServerResponse res)
{
    string cateName = req.params.get("catename", "");
    if (cateName == "")
    {
        return res.writeBody("not found", 404);
    }
    auto articleList = getArticlesByCategoryName(cateName);
    auto categoryList = getCategoryList();

    string html = renderFile!("catearts.tmpl", categoryList, articleList, cateName);
    res.writeBody(html, "text/html; charset=utf-8");
}

/** 
 * 分类列表
 */
void getCategotyList (scope HTTPServerRequest req, scope HTTPServerResponse res)
{
    auto categoryList = getCategoryList();

    Json resData = Json.emptyObject;
    resData["code"] = 200;
    resData["msg"] = "ok";
    resData["data"] = serializeToJson(categoryList);
    res.writeJsonBody(resData);
}
